package com.inno.life.domain.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.inno.life.common.constant.RocketMQConstant;
import com.inno.life.common.context.LoginUserContext;
import com.inno.life.common.entity.PageResult;
import com.inno.life.common.enums.YesOrNoEnum;
import com.inno.life.common.utils.DateUtils;
import com.inno.life.domain.convert.ScheduleManagerConvert;
import com.inno.life.domain.entity.PlannerScheduleManagerBO;
import com.inno.life.domain.service.ScheduleManagerDomainService;
import com.inno.life.infra.basic.entity.PlannerScheduleManager;
import com.inno.life.infra.basic.service.PlannerScheduleManagerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ScheduleManagerDomainServiceImpl implements ScheduleManagerDomainService {

    private final PlannerScheduleManagerService plannerScheduleManagerService;

    private final RocketMQTemplate rocketMQTemplate;


    /**
     * 创建日程计划
     */
    @Override
    public void create(PlannerScheduleManagerBO plannerScheduleManagerBO) {
        PlannerScheduleManager plannerScheduleManager = ScheduleManagerConvert.INSTANCE.convertBOToEntity(plannerScheduleManagerBO);

        plannerScheduleManager.setId(IdUtil.getSnowflakeNextId());

        plannerScheduleManager.setIsDeleted(YesOrNoEnum.NO.getCode());
        plannerScheduleManager.setIsComplete(YesOrNoEnum.NO.getCode());
        plannerScheduleManager.setRemindComplete(YesOrNoEnum.NO.getCode());
        plannerScheduleManager.setCreateBy(plannerScheduleManagerBO.getUserId());
        plannerScheduleManager.setCreateTime(DateUtils.getNowDate());
        this.plannerScheduleManagerService.insert(plannerScheduleManager);

        this.sendMessage(plannerScheduleManager);

    }

    private void sendMessage(PlannerScheduleManager scheduleManager) {
        PlannerScheduleManagerBO plannerScheduleManagerBO = ScheduleManagerConvert.INSTANCE.convertEntityToBO(scheduleManager);
        Date remindStartTime = plannerScheduleManagerBO.getRemindStartTime();

        Date nowDate = DateUtils.getNowDate();
        if (remindStartTime.getTime() < nowDate.getTime()){
            return;
        }
        long delayTime = remindStartTime.getTime() - nowDate.getTime();


        Message message = new Message();
        message.setTopic(RocketMQConstant.TopicConstant.SCHEDULE_MANAGER_REMIND);
        message.setDelayTimeMs(delayTime);
        message.setBody(JSON.toJSONString(plannerScheduleManagerBO).getBytes());
        DefaultMQProducer producer = this.rocketMQTemplate.getProducer();
        try {
            producer.send(message);
        } catch (Exception e) {
            log.error("发送消息失败，原因：{}", e.getMessage());
        }
        log.info("发送消息成功，消息内容：{},{}", JSON.toJSONString(plannerScheduleManagerBO), delayTime);
    }

    /**
     * 日程计划详情
     */
    @Override
    public PlannerScheduleManagerBO queryById(PlannerScheduleManagerBO plannerScheduleManagerBO) {
        PlannerScheduleManager entityResult = this.plannerScheduleManagerService.queryById(plannerScheduleManagerBO.getId());
        return ScheduleManagerConvert.INSTANCE.convertEntityToBO(entityResult);
    }

    /**
     * 编辑日程计划
     */
    @Override
    public void edit(PlannerScheduleManagerBO plannerScheduleManagerBO) {
        PlannerScheduleManager plannerScheduleManager = ScheduleManagerConvert.INSTANCE.convertBOToEntity(plannerScheduleManagerBO);
        plannerScheduleManager.setUpdateBy(LoginUserContext.getUserId());
        plannerScheduleManager.setUpdateTime(DateUtils.getNowDate());
        this.plannerScheduleManagerService.update(plannerScheduleManager);
    }

    /**
     * 删除日程计划
     */
    @Override
    public void delete(PlannerScheduleManagerBO plannerScheduleManagerBO) {
        PlannerScheduleManager plannerScheduleManager = ScheduleManagerConvert.INSTANCE.convertBOToEntity(plannerScheduleManagerBO);
        Date nowDate = DateUtils.getNowDate();
        plannerScheduleManager.setUpdateBy(LoginUserContext.getUserId());
        plannerScheduleManager.setUpdateTime(nowDate);
        plannerScheduleManager.setIsDeleted(YesOrNoEnum.YES.getCode());
        plannerScheduleManager.setDeletedTime(nowDate);
        this.plannerScheduleManagerService.update(plannerScheduleManager);
    }

    /**
     * 分页查询
     */
    @Override
    public PageResult<PlannerScheduleManagerBO> queryPage(PlannerScheduleManagerBO plannerScheduleManagerBO) {
        PlannerScheduleManager plannerScheduleManager = ScheduleManagerConvert.INSTANCE.convertBOToEntity(plannerScheduleManagerBO);
        PageHelper.startPage(plannerScheduleManagerBO.getPage(), plannerScheduleManagerBO.getPageSize());
        Page<PlannerScheduleManager> entityPage = this.plannerScheduleManagerService.queryPage(plannerScheduleManager);
        PageResult<PlannerScheduleManagerBO> entityResult = new PageResult<>();
        entityResult.setTotal(entityPage.getTotal());
        List<PlannerScheduleManagerBO> boList = new LinkedList<>();
        entityPage.getResult().forEach(item -> boList.add(ScheduleManagerConvert.INSTANCE.convertEntityToBO(item)));
        entityResult.setList(boList);
        return entityResult;
    }

    @Override
    public PageResult<PlannerScheduleManagerBO> queryRecyclePage(PlannerScheduleManagerBO plannerScheduleManagerBO) {
        PlannerScheduleManager plannerScheduleManager = ScheduleManagerConvert.INSTANCE.convertBOToEntity(plannerScheduleManagerBO);
        plannerScheduleManager.setIsDeleted(YesOrNoEnum.YES.getCode());
        PageHelper.startPage(plannerScheduleManagerBO.getPage(), plannerScheduleManagerBO.getPageSize());
        Page<PlannerScheduleManager> entityPage = this.plannerScheduleManagerService.queryPage(plannerScheduleManager);
        PageResult<PlannerScheduleManagerBO> entityResult = new PageResult<>();
        entityResult.setTotal(entityPage.getTotal());
        List<PlannerScheduleManagerBO> boList = new LinkedList<>();
        entityPage.getResult().forEach(item -> boList.add(ScheduleManagerConvert.INSTANCE.convertEntityToBO(item)));
        entityResult.setList(boList);
        return entityResult;
    }

    @Override
    public List<PlannerScheduleManager> getPendingReminders(Date nowDate) {
        return this.plannerScheduleManagerService.getPendingReminders(nowDate);
    }
}
